import pygame
import random
import os

class Game:
    def __init__(self,stateDict,startState):

        self.screen = pygame.display.get_surface()
        self.clock = pygame.time.Clock()
        self.keys=pygame.key.get_pressed()
        self.stateDict=stateDict
        self.state=self.stateDict[startState]

    def update(self):
        if self.state.finished:
            nextState=self.state.next
            self.state.finished=False
            self.state=self.stateDict[nextState]
        self.state.update(self.screen, self.keys)

    def run(self):#GRAPHICS
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.display.quit()
                elif event.type == pygame.KEYDOWN:
                    self.keys=pygame.key.get_pressed()
                elif event.type == pygame.KEYUP:
                    self.keys=pygame.key.get_pressed()
             # self.screen.blit(image,(0,0))
            self.update()

            pygame.display.update()
            self.clock.tick(60)
            # if event.key == pygame.K_ESCAPE:
            #     pygame.quit()
            #     sys.exit()
def load_graphics(path,accept=('.png','.jpg')):
    graphics={}
    for pic in os.listdir(path):
        name, ext = os.path.splitext(pic)
        if ext.lower() in accept:
            img=pygame.image.load(os.path.join(path,pic))
            if img.get_alpha():
                img=img.convert_alpha()
            else:img=img.convert()
            graphics[name]=img
    return graphics

def get_image(sheet,x,y,width,height,colorkey,scale):
    image=pygame.Surface((width,height))
    image.blit(sheet,(0,0),(x,y,width,height))
    image.set_colorkey(colorkey)
    image=pygame.transform.scale(image,(int(width*scale),int(height*scale)))
    return image
